package com.ljc.cjgl.service.bbs;

import com.ljc.cjgl.entitiy.Course;
import com.ljc.cjgl.entitiy.bbs.Article;
import com.ljc.cjgl.entitiy.bbs.Comment;
import com.ljc.cjgl.form.bbs.ArticleForm;
import com.ljc.cjgl.repository.CourseRepository;
import com.ljc.cjgl.repository.bbs.ArticleRepository;
import com.ljc.cjgl.repository.bbs.CommentRepository;
import com.ljc.cjgl.security.SysUser;
import com.ljc.cjgl.security.SysUserRepository;
import com.ljc.cjgl.security.User2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author: JavaJun
 * Date: 2019-04-28 16:08
 */
@Service
public class ArticleService {

    @Autowired
    private ArticleRepository articleRepository;

    @Autowired
    private SysUserRepository sysUserRepository;

    @Autowired
    private CourseRepository courseRepository;

    @Autowired
    private CommentRepository commentRepository;

    public Article findById(Integer id) {
        Optional<Article> optional = articleRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }

    public Page<Article> findAll(Specification<Article> specification, Pageable pageRequest) {
        return articleRepository.findAll(specification, pageRequest);
    }

    @Transactional
    public synchronized Article save(ArticleForm form) {
        Article model = new Article();
        Integer id = form.getId();
        if (id != null) {
            Optional<Article> optional = articleRepository.findById(id);
            if (optional.isPresent()) {
                model = optional.get();
            }
        }
        if (form.getCourseId() != null) {
            Optional<Course> optional = courseRepository.findById(form.getCourseId());
            if (optional.isPresent()) {
                model.setCourse(optional.get());
            }
        }
        model.setTitle(form.getTitle());
        model.setContent(form.getContent());
        // model.setCourse(course);
        model.setInTime(new Date());
        /* 获取当前的登录用户的 username */
        User2 principal = (User2) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = principal.getUsername();
        SysUser user = sysUserRepository.findByUsername(username);
        model.setSysUser(user);
        Article save = articleRepository.save(model);
        return save;
    }

    public List<Article> findBySysUserUsername(String username) {
        return articleRepository.findBySysUserUsername(username);
    }

    /**
     * 批量删除
     * @param articles
     * @return
     */
    public synchronized boolean delete(List<Article> articles) {
        try {
            for (Article s : articles) {
                List<Comment> byArticleId = commentRepository.findByArticleId(s.getId());
                for (Comment comment : byArticleId) {
                    commentRepository.delete(comment);
                }
                articleRepository.deleteById(s.getId());
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

}
